def simple(n):
    if n % 2 == 0:
        return n == 2
    a = 3
    while a * a <= n and n % a != 0:
        a += 2
    return a * a > n
    
num = int(input())

if num > 999 and num < 10000: 
    g = int(str(num)[:3])

    numbers = [num, int(str(num)[0]), int(str(num)[1]), int(str(num)[2]), int(str(num)[3]), 
    int(str(num)[:2]), int(str(num)[-2:]), int(str(num)[-3:]), int(str(g)[-2:]),  ]
    numbers.append(g)
    
    top = []

    for number in numbers:
        if simple(number) == True:
            top.append(number)

    if len(top) == 0:
        print(0)
        
    else:

        maxs = max(top)
        print(maxs)

else:
    print("0")